/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Contacs.java
 *
 * Created on Sep 25, 2011, 6:03:28 AM
 */
package azhar.view;

import azhar.com.HTMLText;
import azhar.com.PopupMenuMessage;
import azhar.com.Separate;
import azhar.com.TableOperation;
import azhar.persistance.Draft;
import azhar.persistance.Inbox;
import azhar.persistance.Outbox;
import azhar.persistance.SentItem;
import azhar.persistance.control.exceptions.NonexistentEntityException;
import azhar.view.control.CreateMessage;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Al-Insan
 */
public class Messages extends javax.swing.JPanel {

    private PopupMenuMessage showPop;
    private ArrayList<azhar.com.Messages> messages = new ArrayList<azhar.com.Messages>();
    private DefaultTableModel model;
    private TableOperation tabOp = new TableOperation();
    private Main main;
    private ArrayList idInbox = new ArrayList();
    private ArrayList idOutbox = new ArrayList();
    private ArrayList idDraft = new ArrayList();
    private ArrayList idSent = new ArrayList();

    /** Creates new form Contacs */
    public Messages(Main m) {
        initComponents();
        main = m;
        initTable();
    }

    public void setData(String key, String[] data) {
        if (key.equalsIgnoreCase("outbox")) {
            tabOp.insertData(outboxTable, data);
        } else if (key.equalsIgnoreCase("inbox")) {
            tabOp.insertData(inboxTable, data);
        } else if (key.equalsIgnoreCase("sentItem")) {
            tabOp.insertData(sentItemTable, data);
        } else if (key.equalsIgnoreCase("draft")) {
            tabOp.insertData(draftTable, data);
        }
    }

    public void initTable() {
        for (int i = main.dtInbox.length - 1; i > -1; i--) {
            String[] data = new String[main.dtInbox[i].size()];
            idInbox.add(0, main.dtInbox[i].get(0));
            for (int j = 1; j < data.length; j++) {
                data[j - 1] = (String) main.dtInbox[i].get(j);
            }
            setData("inbox", data);
        }
        for (int i = main.dtOutbox.length - 1; i > -1; i--) {
            String[] data = new String[main.dtOutbox[i].size()];
            idOutbox.add(0, main.dtOutbox[i].get(0));
            for (int j = 1; j < data.length; j++) {
                data[j - 1] = (String) main.dtOutbox[i].get(j);
            }
            setData("outbox", data);
        }
        for (int i = main.dtDraft.length - 1; i > -1; i--) {
            String[] data = new String[main.dtDraft[i].size()];
            idDraft.add(0, main.dtDraft[i].get(0));
            for (int j = 1; j < data.length; j++) {
                data[j - 1] = (String) main.dtDraft[i].get(j);
            }
            setData("draft", data);
        }
        for (int i = main.dtSentItem.length - 1; i > -1; i--) {
            String[] data = new String[main.dtSentItem[i].size()];
            idSent.add(0, main.dtSentItem[i].get(0));
            for (int j = 1; j < data.length; j++) {
                data[j - 1] = (String) main.dtSentItem[i].get(j);
            }
            setData("sentItem", data);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabMessage = new javax.swing.JTabbedPane();
        panelIndox = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        delete = new javax.swing.JButton();
        newMessage = new javax.swing.JButton();
        reply = new javax.swing.JButton();
        forward = new javax.swing.JButton();
        edit = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        inboxTable = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        textInbox = new javax.swing.JTextPane();
        panelDraft = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        delete3 = new javax.swing.JButton();
        send = new javax.swing.JButton();
        edit3 = new javax.swing.JButton();
        jPanel10 = new javax.swing.JPanel();
        jScrollPane7 = new javax.swing.JScrollPane();
        draftTable = new javax.swing.JTable();
        jScrollPane8 = new javax.swing.JScrollPane();
        textDraft = new javax.swing.JTextPane();
        panelOutbox = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        delete1 = new javax.swing.JButton();
        reply1 = new javax.swing.JButton();
        edit1 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        outboxTable = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        textOutbox = new javax.swing.JTextPane();
        panelSentItem = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        delete2 = new javax.swing.JButton();
        forward2 = new javax.swing.JButton();
        edit2 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        sentItemTable = new javax.swing.JTable();
        jScrollPane6 = new javax.swing.JScrollPane();
        textSentItem = new javax.swing.JTextPane();

        setPreferredSize(new java.awt.Dimension(701, 333));
        setLayout(new java.awt.CardLayout());

        tabMessage.setTabPlacement(javax.swing.JTabbedPane.LEFT);

        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        delete.setText("Delete");
        delete.setOpaque(false);
        delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteActionPerformed(evt);
            }
        });
        jPanel3.add(delete, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 7, -1, -1));

        newMessage.setText("New Message");
        newMessage.setOpaque(false);
        newMessage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMessageActionPerformed(evt);
            }
        });
        jPanel3.add(newMessage, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 7, -1, -1));

        reply.setText("Reply");
        reply.setOpaque(false);
        reply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                replyActionPerformed(evt);
            }
        });
        jPanel3.add(reply, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 7, -1, -1));

        forward.setText("Forward");
        forward.setOpaque(false);
        forward.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                forwardActionPerformed(evt);
            }
        });
        jPanel3.add(forward, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 7, -1, -1));

        edit.setText("Edit");
        edit.setOpaque(false);
        edit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editActionPerformed(evt);
            }
        });
        jPanel3.add(edit, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 7, -1, -1));

        jPanel4.setLayout(new java.awt.GridLayout(2, 1, 0, 5));

        inboxTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Sender", "Message", "Sent Time", "Message Center"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        inboxTable.getTableHeader().setReorderingAllowed(false);
        inboxTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                inboxTableMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                inboxTableMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(inboxTable);
        inboxTable.getColumnModel().getColumn(0).setResizable(false);
        inboxTable.getColumnModel().getColumn(0).setPreferredWidth(60);
        inboxTable.getColumnModel().getColumn(1).setResizable(false);
        inboxTable.getColumnModel().getColumn(1).setPreferredWidth(170);
        inboxTable.getColumnModel().getColumn(2).setResizable(false);
        inboxTable.getColumnModel().getColumn(2).setPreferredWidth(50);
        inboxTable.getColumnModel().getColumn(3).setResizable(false);
        inboxTable.getColumnModel().getColumn(3).setPreferredWidth(60);
        inboxTable.getAccessibleContext().setAccessibleName("inbox");

        jPanel4.add(jScrollPane1);

        textInbox.setContentType("text/html");
        textInbox.setEditable(false);
        jScrollPane2.setViewportView(textInbox);

        jPanel4.add(jScrollPane2);

        javax.swing.GroupLayout panelIndoxLayout = new javax.swing.GroupLayout(panelIndox);
        panelIndox.setLayout(panelIndoxLayout);
        panelIndoxLayout.setHorizontalGroup(
            panelIndoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
        );
        panelIndoxLayout.setVerticalGroup(
            panelIndoxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelIndoxLayout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
        );

        tabMessage.addTab("Inbox", panelIndox);

        jPanel9.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        delete3.setText("Delete");
        delete3.setOpaque(false);
        delete3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete3ActionPerformed(evt);
            }
        });
        jPanel9.add(delete3, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 7, -1, -1));

        send.setText("Send");
        send.setOpaque(false);
        send.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendActionPerformed(evt);
            }
        });
        jPanel9.add(send, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 7, -1, -1));

        edit3.setText("Edit");
        edit3.setOpaque(false);
        edit3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edit3ActionPerformed(evt);
            }
        });
        jPanel9.add(edit3, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 7, -1, -1));

        jPanel10.setLayout(new java.awt.GridLayout(2, 1, 0, 5));

        draftTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Recipient", "Message"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        draftTable.getTableHeader().setReorderingAllowed(false);
        draftTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                draftTableMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                draftTableMouseReleased(evt);
            }
        });
        jScrollPane7.setViewportView(draftTable);
        draftTable.getColumnModel().getColumn(0).setResizable(false);
        draftTable.getColumnModel().getColumn(0).setPreferredWidth(60);
        draftTable.getColumnModel().getColumn(1).setResizable(false);
        draftTable.getColumnModel().getColumn(1).setPreferredWidth(170);
        draftTable.getAccessibleContext().setAccessibleName("draft");

        jPanel10.add(jScrollPane7);

        textDraft.setContentType("text/html");
        jScrollPane8.setViewportView(textDraft);

        jPanel10.add(jScrollPane8);

        javax.swing.GroupLayout panelDraftLayout = new javax.swing.GroupLayout(panelDraft);
        panelDraft.setLayout(panelDraftLayout);
        panelDraftLayout.setHorizontalGroup(
            panelDraftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
            .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
        );
        panelDraftLayout.setVerticalGroup(
            panelDraftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelDraftLayout.createSequentialGroup()
                .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
        );

        tabMessage.addTab("Draft", panelDraft);

        jPanel5.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        delete1.setText("Delete");
        delete1.setOpaque(false);
        delete1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete1ActionPerformed(evt);
            }
        });
        jPanel5.add(delete1, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 7, -1, -1));

        reply1.setText("Send Again");
        reply1.setOpaque(false);
        reply1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reply1ActionPerformed(evt);
            }
        });
        jPanel5.add(reply1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 7, -1, -1));

        edit1.setText("Edit");
        edit1.setOpaque(false);
        edit1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edit1ActionPerformed(evt);
            }
        });
        jPanel5.add(edit1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 7, -1, -1));

        jPanel6.setLayout(new java.awt.GridLayout(2, 1, 0, 5));

        outboxTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Recipient", "Message", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        outboxTable.getTableHeader().setReorderingAllowed(false);
        outboxTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                outboxTableMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                outboxTableMouseReleased(evt);
            }
        });
        jScrollPane3.setViewportView(outboxTable);
        outboxTable.getColumnModel().getColumn(0).setResizable(false);
        outboxTable.getColumnModel().getColumn(0).setPreferredWidth(60);
        outboxTable.getColumnModel().getColumn(1).setResizable(false);
        outboxTable.getColumnModel().getColumn(1).setPreferredWidth(200);
        outboxTable.getColumnModel().getColumn(2).setResizable(false);
        outboxTable.getColumnModel().getColumn(2).setPreferredWidth(30);
        outboxTable.getAccessibleContext().setAccessibleName("outbox");

        jPanel6.add(jScrollPane3);

        textOutbox.setContentType("text/html");
        jScrollPane4.setViewportView(textOutbox);

        jPanel6.add(jScrollPane4);

        javax.swing.GroupLayout panelOutboxLayout = new javax.swing.GroupLayout(panelOutbox);
        panelOutbox.setLayout(panelOutboxLayout);
        panelOutboxLayout.setHorizontalGroup(
            panelOutboxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
        );
        panelOutboxLayout.setVerticalGroup(
            panelOutboxLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelOutboxLayout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
        );

        tabMessage.addTab("Outbox", panelOutbox);

        jPanel7.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        delete2.setText("Delete");
        delete2.setOpaque(false);
        delete2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delete2ActionPerformed(evt);
            }
        });
        jPanel7.add(delete2, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 7, -1, -1));

        forward2.setText("Forward");
        forward2.setOpaque(false);
        forward2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                forward2ActionPerformed(evt);
            }
        });
        jPanel7.add(forward2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 7, -1, -1));

        edit2.setText("Edit");
        edit2.setOpaque(false);
        edit2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edit2ActionPerformed(evt);
            }
        });
        jPanel7.add(edit2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 7, -1, -1));

        jPanel8.setLayout(new java.awt.GridLayout(2, 1, 0, 5));

        sentItemTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Recipient", "Message", "Status", "Sent Time"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        sentItemTable.getTableHeader().setReorderingAllowed(false);
        sentItemTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sentItemTableMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                sentItemTableMouseReleased(evt);
            }
        });
        jScrollPane5.setViewportView(sentItemTable);
        sentItemTable.getColumnModel().getColumn(0).setResizable(false);
        sentItemTable.getColumnModel().getColumn(0).setPreferredWidth(60);
        sentItemTable.getColumnModel().getColumn(1).setResizable(false);
        sentItemTable.getColumnModel().getColumn(1).setPreferredWidth(170);
        sentItemTable.getColumnModel().getColumn(2).setResizable(false);
        sentItemTable.getColumnModel().getColumn(2).setPreferredWidth(30);
        sentItemTable.getColumnModel().getColumn(3).setResizable(false);
        sentItemTable.getAccessibleContext().setAccessibleName("sentItem");

        jPanel8.add(jScrollPane5);

        textSentItem.setContentType("text/html");
        jScrollPane6.setViewportView(textSentItem);

        jPanel8.add(jScrollPane6);

        javax.swing.GroupLayout panelSentItemLayout = new javax.swing.GroupLayout(panelSentItem);
        panelSentItem.setLayout(panelSentItemLayout);
        panelSentItemLayout.setHorizontalGroup(
            panelSentItemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, 649, Short.MAX_VALUE)
        );
        panelSentItemLayout.setVerticalGroup(
            panelSentItemLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSentItemLayout.createSequentialGroup()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
        );

        tabMessage.addTab("Sent Item", panelSentItem);

        add(tabMessage, "card2");
        tabMessage.getAccessibleContext().setAccessibleName("tabMessage");
    }// </editor-fold>//GEN-END:initComponents

    private void inboxTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_inboxTableMouseReleased
        if (evt.getButton() == evt.BUTTON3) {
            if (inboxTable.getSelectedRow() != -1) {
                if (evt.isPopupTrigger()) {
                    new PopupMenuMessage(inboxTable, main, this).show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        }
    }//GEN-LAST:event_inboxTableMouseReleased

    private void draftTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_draftTableMouseReleased
        if (evt.getButton() == evt.BUTTON3) {
            if (draftTable.getSelectedRow() != -1) {
                if (evt.isPopupTrigger()) {
                    new PopupMenuMessage(draftTable, main, this).show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        }
    }//GEN-LAST:event_draftTableMouseReleased

    private void outboxTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_outboxTableMouseReleased
        if (evt.getButton() == evt.BUTTON3) {
            if (outboxTable.getSelectedRow() != -1) {
                if (evt.isPopupTrigger()) {
                    new PopupMenuMessage(outboxTable, main, this).show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        }
    }//GEN-LAST:event_outboxTableMouseReleased

    private void sentItemTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sentItemTableMouseReleased
        if (evt.getButton() == evt.BUTTON3) {
            if (sentItemTable.getSelectedRow() != -1) {
                if (evt.isPopupTrigger()) {
                    new PopupMenuMessage(sentItemTable, main, this).show(evt.getComponent(), evt.getX(), evt.getY());
                }
            }
        }
    }//GEN-LAST:event_sentItemTableMouseReleased

    private void newMessageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMessageActionPerformed
        new CreateMessage(main, true).setVisible(true);
    }//GEN-LAST:event_newMessageActionPerformed

    private void replyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_replyActionPerformed
        if (inboxTable.getSelectedRowCount() > 0 && inboxTable.getSelectedRowCount() < 2) {
            String number = new Separate().separated(tabOp.getValues(inboxTable, "Sender"));
            new CreateMessage(main, true, number).setVisible(true);
        }
    }//GEN-LAST:event_replyActionPerformed

    private void forwardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_forwardActionPerformed
        if (inboxTable.getSelectedRowCount() > 0 && inboxTable.getSelectedRowCount() < 2) {
            new CreateMessage(main, false, "", inboxTable.getValueAt(inboxTable.getSelectedRow(), 1)).setVisible(true);
        }
    }//GEN-LAST:event_forwardActionPerformed

    private void editActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editActionPerformed
        if (inboxTable.getSelectedRowCount() > 0 && inboxTable.getSelectedRowCount() < 2) {
            new CreateMessage(main, true, "", inboxTable.getValueAt(inboxTable.getSelectedRow(), 1)).setVisible(true);
        }
    }//GEN-LAST:event_editActionPerformed

    private void deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteActionPerformed
        for (int i = inboxTable.getSelectedRows().length - 1; i >= 0; i--) {
            deleteData(inboxTable.getAccessibleContext().getAccessibleName(), inboxTable.getSelectedRows()[i]);
        }
        tabOp.deleteData(inboxTable, inboxTable.getSelectedRows());
        clearText();
    }//GEN-LAST:event_deleteActionPerformed

    private void sendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendActionPerformed
        try {
            for (int i = draftTable.getSelectedRows().length - 1; i > -1; i--) {
                messages.add(new azhar.com.Messages("" + draftTable.getValueAt(draftTable.getSelectedRows()[i], 0), "" + draftTable.getValueAt(draftTable.getSelectedRows()[i], 1)));
            }
            main.sendMessage(messages);
            messages.clear();
        } catch (Exception ex) {
            Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_sendActionPerformed

    private void edit3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edit3ActionPerformed
        if (draftTable.getSelectedRowCount() > 0 && draftTable.getSelectedRowCount() < 2) {
            new CreateMessage(main, true, "", draftTable.getValueAt(draftTable.getSelectedRow(), 1)).setVisible(true);
        }
    }//GEN-LAST:event_edit3ActionPerformed

    private void delete3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete3ActionPerformed
        for (int i = draftTable.getSelectedRows().length - 1; i >= 0; i--) {
            deleteData(draftTable.getAccessibleContext().getAccessibleName(), draftTable.getSelectedRows()[i]);
        }
        tabOp.deleteData(draftTable, draftTable.getSelectedRows());
        clearText();
    }//GEN-LAST:event_delete3ActionPerformed

    private void reply1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reply1ActionPerformed
        try {
            for (int i = outboxTable.getSelectedRowCount() - 1; i > -1; i--) {
                try {
                    messages.add(new azhar.com.Messages("" + outboxTable.getValueAt(outboxTable.getSelectedRows()[i], 0), "" + outboxTable.getValueAt(outboxTable.getSelectedRows()[i], 1)));
                    deleteData(outboxTable.getAccessibleContext().getAccessibleName(), outboxTable.getSelectedRows()[i]);
                } catch (Exception ex) {
                    Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            main.sendMessage(messages);
            messages.clear();
            tabOp.deleteData(outboxTable, outboxTable.getSelectedRows());
        } catch (Exception ex) {
            Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_reply1ActionPerformed

    private void edit1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edit1ActionPerformed
        if (outboxTable.getSelectedRowCount() > 0 && outboxTable.getSelectedRowCount() < 2) {
            new CreateMessage(main, true, "", outboxTable.getValueAt(outboxTable.getSelectedRow(), 1)).setVisible(true);
        }
    }//GEN-LAST:event_edit1ActionPerformed

    private void delete1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete1ActionPerformed
        for (int i = outboxTable.getSelectedRows().length - 1; i >= 0; i--) {
            deleteData(outboxTable.getAccessibleContext().getAccessibleName(), outboxTable.getSelectedRows()[i]);
        }
        tabOp.deleteData(outboxTable, outboxTable.getSelectedRows());
        clearText();
    }//GEN-LAST:event_delete1ActionPerformed

    private void forward2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_forward2ActionPerformed
        if (sentItemTable.getSelectedRowCount() > 0 && sentItemTable.getSelectedRowCount() < 2) {
            new CreateMessage(main, false, "", sentItemTable.getValueAt(sentItemTable.getSelectedRow(), 1)).setVisible(true);
        }
    }//GEN-LAST:event_forward2ActionPerformed

    private void edit2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edit2ActionPerformed
        if (sentItemTable.getSelectedRowCount() > 0 && sentItemTable.getSelectedRowCount() < 2) {
            new CreateMessage(main, true, "", sentItemTable.getValueAt(sentItemTable.getSelectedRow(), 1)).setVisible(true);
        }
    }//GEN-LAST:event_edit2ActionPerformed

    private void delete2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete2ActionPerformed
        for (int i = sentItemTable.getSelectedRows().length - 1; i >= 0; i--) {
            deleteData(sentItemTable.getAccessibleContext().getAccessibleName(), sentItemTable.getSelectedRows()[i]);
        }
        tabOp.deleteData(sentItemTable, sentItemTable.getSelectedRows());
        clearText();
    }//GEN-LAST:event_delete2ActionPerformed

    private void inboxTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_inboxTableMouseClicked
        if (inboxTable.getSelectedRowCount() < 2) {
            String sender = (String) inboxTable.getValueAt(inboxTable.getSelectedRow(), 0);
            String text = (String) inboxTable.getValueAt(inboxTable.getSelectedRow(), 1);
            String time = (String) inboxTable.getValueAt(inboxTable.getSelectedRow(), 2);
            String smsc = (String) inboxTable.getValueAt(inboxTable.getSelectedRow(), 3);
            textInbox.setText(new HTMLText().getText(sender, text, time, smsc));
            int i = Integer.parseInt("" + idInbox.get(inboxTable.getSelectedRow()));
            String id = "" + main.dbControl.inbox.findInbox(i).getStatus();
            if (id.equals("0")) {
                try {
                    main.dbControl.inbox.edit(new Inbox(i, sender, text, time, smsc, 1));
                    main.setMessageIndicator(-1);
                } catch (Exception ex) {
                }
            }
        }
    }//GEN-LAST:event_inboxTableMouseClicked

    private void draftTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_draftTableMouseClicked
        if (evt.getButton() == evt.BUTTON1) {
            String recipient = (String) draftTable.getValueAt(draftTable.getSelectedRow(), 0);
            String text = (String) draftTable.getValueAt(draftTable.getSelectedRow(), 1);
            textDraft.setText(new HTMLText().getText(recipient, text));
        }
    }//GEN-LAST:event_draftTableMouseClicked

    private void outboxTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_outboxTableMouseClicked
        if (evt.getButton() == evt.BUTTON1) {
            String recipient = (String) outboxTable.getValueAt(outboxTable.getSelectedRow(), 0);
            String text = (String) outboxTable.getValueAt(outboxTable.getSelectedRow(), 1);
            String status = (String) outboxTable.getValueAt(outboxTable.getSelectedRow(), 2);
            textOutbox.setText(new HTMLText().getText(recipient, text, status));
        }
    }//GEN-LAST:event_outboxTableMouseClicked

    private void sentItemTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sentItemTableMouseClicked
        if (evt.getButton() == evt.BUTTON1) {
            String recipient = (String) sentItemTable.getValueAt(sentItemTable.getSelectedRow(), 0);
            String text = (String) sentItemTable.getValueAt(sentItemTable.getSelectedRow(), 1);
            String status = (String) sentItemTable.getValueAt(sentItemTable.getSelectedRow(), 2);
            String time = (String) sentItemTable.getValueAt(sentItemTable.getSelectedRow(), 3);
            textSentItem.setText(new HTMLText().getText(recipient, text, status, time, 1));
        }
    }//GEN-LAST:event_sentItemTableMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton delete;
    private javax.swing.JButton delete1;
    private javax.swing.JButton delete2;
    private javax.swing.JButton delete3;
    private javax.swing.JTable draftTable;
    private javax.swing.JButton edit;
    private javax.swing.JButton edit1;
    private javax.swing.JButton edit2;
    private javax.swing.JButton edit3;
    private javax.swing.JButton forward;
    private javax.swing.JButton forward2;
    private javax.swing.JTable inboxTable;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JButton newMessage;
    private javax.swing.JTable outboxTable;
    private javax.swing.JPanel panelDraft;
    private javax.swing.JPanel panelIndox;
    private javax.swing.JPanel panelOutbox;
    private javax.swing.JPanel panelSentItem;
    private javax.swing.JButton reply;
    private javax.swing.JButton reply1;
    private javax.swing.JButton send;
    private javax.swing.JTable sentItemTable;
    private javax.swing.JTabbedPane tabMessage;
    private javax.swing.JTextPane textDraft;
    private javax.swing.JTextPane textInbox;
    private javax.swing.JTextPane textOutbox;
    private javax.swing.JTextPane textSentItem;
    // End of variables declaration//GEN-END:variables

    public void clearText() {
        textInbox.setText("");
        textDraft.setText("");
        textOutbox.setText("");
        textSentItem.setText("");
    }

    public void readInbox() {
        tabMessage.setSelectedIndex(0);
    }

    public void addData(String key, String[] data) {
        if (key.equalsIgnoreCase("outbox")) {
            main.dbControl.outbox.create(new Outbox(0, data[0], data[1], data[2]));
            tabOp.insertData(outboxTable, data);
            idOutbox.add(0, main.dbControl.getLastIdOutbox());
        } else if (key.equalsIgnoreCase("inbox")) {
            main.dbControl.inbox.create(new Inbox(0, data[0], data[1], data[2], data[3], 0));
            tabOp.insertData(inboxTable, data);
            idInbox.add(0, main.dbControl.getLastIdInbox());
        } else if (key.equalsIgnoreCase("sentItem")) {
            main.dbControl.sentItem.create(new SentItem(0, data[0], data[1], data[2], data[3]));
            tabOp.insertData(sentItemTable, data);
            idSent.add(0, main.dbControl.getLastIdSent());
        } else if (key.equalsIgnoreCase("draft")) {
            main.dbControl.draft.create(new Draft(0, data[0], data[1]));
            tabOp.insertData(draftTable, data);
            idDraft.add(0, main.dbControl.getLastIdDraft());
        }
    }

    public void deleteData(String key, int i) {
        System.out.println(i);
        if (key.equals("inbox")) {
            String n = "" + idInbox.get(i);
            try {
                main.dbControl.inbox.destroy(Integer.parseInt(n));
                System.out.println(idInbox.get(i));
                idInbox.remove(i);
            } catch (NonexistentEntityException ex) {
                JOptionPane.showMessageDialog(panelDraft, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        } else if (key.equals("outbox")) {
            String n = "" + idOutbox.get(i);
            try {
                main.dbControl.outbox.destroy(Integer.parseInt(n));
                idOutbox.remove(i);
            } catch (NonexistentEntityException ex) {
                JOptionPane.showMessageDialog(panelDraft, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        } else if (key.equals("draft")) {
            String n = "" + idDraft.get(i);
            try {
                main.dbControl.draft.destroy(Integer.parseInt(n));
                idDraft.remove(i);
            } catch (NonexistentEntityException ex) {
                JOptionPane.showMessageDialog(panelDraft, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        } else if (key.equals("sentItem")) {
            String n = "" + idSent.get(i);
            try {
                main.dbControl.sentItem.destroy(Integer.parseInt(n));
                idSent.remove(i);
            } catch (NonexistentEntityException ex) {
                JOptionPane.showMessageDialog(panelDraft, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
